layui.use(['form', 'table'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer=layui.layer;
    //form.render();
    $.ajaxSetup({
        // 发送cookie
        xhrFields: {
            withCredentials: true
        },
        // 请求发送前
        beforeSend: function () {
            // 发送请求前，可以对data、url等处理
        },
        // 请求返回
        complete: function () {
            // 返回数据，根据数据调转页面等
        }
    });
    // url=devUrl + '',
    $.getJSON(
        devUrl + '/api/device/admin/getBackDeviceList',
        function (result) {
            $("#devHeadCount").html(result.data.count);
            $("#normal").html(result.data.normal);
            $("#fault").html(result.data.fault);
            $("#offLine").html(result.data.offLine);
            $("#stockout").html(result.data.stockout);
        }
    );
    $.getJSON(
        devUrl + '/api/device/admin/getDeviceCriteriaQuerySelect',
        function (result) {
            let str = "<option value='0'>请选择</option>";
            let ves = result.data.venuesList;
            //alert(ves[1].venuesid);
            for (let i = 0; i < ves.length; i++) {
                str += "<option value='"+ves[i].venuesid+"'>"+ves[i].venuesname+"</option>";
            }
            $("#venues").html(str);

            str = "<option value='0'>请选择</option>";
            let ves2 = result.data.operatorsList;
            //alert(ves[1].venuesid);
            for (let i = 0; i < ves2.length; i++) {
                str += "<option value='"+ves2[i].managerid+"'>"+ves2[i].username+"</option>";
            }
            $("#operators").html(str);

            str = "<option value='0'>请选择</option>";
            let ves3 = result.data.sortingList;
            //alert(ves[1].venuesid);
            for (let i = 0; i < ves3.length; i++) {
                str += "<option value='"+ves3[i].managerid+"'>"+ves3[i].username+"</option>";
            }

            $("#sorting").html(str);

            str = "<option value='0'>请选择</option>";
            let ves4 = result.data.partnersList;
            //alert(ves[1].venuesid);
            for (let i = 0; i < ves4.length; i++) {
                str += "<option value='"+ves4[i].partnersid+"'>"+ves4[i].username+"</option>";
            }
            // alert(str);
            $("#partners").html(str);
            layui.form.render("select");
        }
    );


    $("#plsj").click(function () {
        let index = layer.open({
            title: '添加设备',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['36%', '54%'],
            content: 'DeviceBatchUpgrading.html',
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
    });

    $("#devAdd").click(function () {
        let index = layer.open({
            title: '添加设备',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['81%', '81%'],
            content: 'DevAdd.html',
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
    });

    table.render({
        elem: '#currentTableId',
        url: devUrl + '/api/device/admin/getBackDeviceList',
        method: "get",
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            {type: "checkbox", width: 50},
            {field: 'deviceid', minWidth:100, title: '设备编号', sort: true},
            {field: 'devicename', minWidth:100, title: '设备名称'},
            {field: 'deviceqrcode', minWidth:100, title: '二维码', sort: true,
                templet:'<div><img src="http://localhost:7001/{{d.deviceqrcode}}" width="36px" height="36px" ></div>'},
            {field: 'venuesname', minWidth:100,title: '场地名称'},
            {field: 'dz', title: '设备地址', minWidth: 150},
            {field: 'sortingname', title:'所属分拣中心', minWidth: 100},
            {field: 'operatorsname', title:'所属运营商', minWidth: 100},
            {field: 'partnersName', title:'所属合伙人', minWidth: 100},
            {field: 'spillover', title:'满溢度', minWidth: 100,
                templet: function (data) {
                    return data.spillover + '%';
                }},
            {field: 'isstock', title:'商品库存', minWidth: 100,
                templet: function (data) {
                    if(data.isstock === 1){
                        return '正常';
                    } else {
                        return "<span style='color: yellow'>缺货</span>";
                    }
                }},//设备状态 1.正常 2.故障red 3.离线yel
            {field: 'devicestatus', title:'设备状态', minWidth: 100,
                templet: function (data) {
                    let sp = '';
                    switch (data.devicestatus) {
                        case 1:
                            sp = '正常';
                            break;
                        case 2:
                            sp = "<a style='color:red;'>故障</a>";
                            break;
                        default:
                            return "<span style='color: yellow'>离线</span>";
                            break;
                    }
                    return sp;
                }},//是否禁用 1 禁用 2 未禁用
            {field: 'isflag', title:'营业状态', minWidth: 100,
                templet: function (data) {
                    if(data.isflag === 2) {
                        return "正常";
                    } else {
                        return "<span style='color: red'>禁用</span>";
                    }
                }},
            /*{field: 'isoff', minWidth:100, title: '状态', sort: true,templet:function(data){
                if(data.isoff==1){
                    return "上架";
                }else{
                    return "下架";
                }
            }},*/
            {field: 'ctime', minWidth:100, title: '创建时间', sort: true},
            {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]]
        ,response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            ,statusCode: '200' //规定成功的状态码，默认：0
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        },
        limits: [5, 10, 15, 20],
        limit: 5,
        page: true,
        //skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        let result = JSON.stringify(data.field);
        /*layer.alert(result, {
            title: '最终的搜索信息'
        });*/
        //执行搜索重载
        table.reload('currentTableId', {
            page:{
                curr:1//将页数重新
            },
            //异步访问表格的查询接口路径
            url: devUrl + '/api/device/admin/getBackDeviceList',
            where:{
                /*给访问的控制类增加参数,右边的是前端查询条件框的名称,左边是后端的参数名称*/
                deviceid:data.field.deviceid,
                devicename:data.field.devicename,
                venuesid:data.field.venues.selected,
                dz:data.field.dz,
                spillover:data.field.spillover,

                //flatformid:data.field.flatformId,
            }
        }, 'data');
        return false;
    });

    /*
     * toolbar监听事件
     /*     */
    table.on('tool(currentTableId)', function (obj) {
        let data = obj.data;
        if (obj.event === 'edit'){
            let index = layer.open({
                title: '编辑设备',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: 'DevAdd.html',
                success : function(layero, index){
                    let body = layui.layer.getChildFrame('body', index);
                    body.contents().find("[name=deviceid]").val(data.deviceid);
                    form.render();
                }
            });
            layer.full(index);
            return false;
        }
        if(obj.event === 'delete') {
            if(confirm("确认要删除吗?")) {
                $.ajax({
                    url: devUrl + '/api/device/admin/delDevice',
                    type: "delete",
                    data:{deviceId:data.deviceid},
                    dataType:"json",
                    success: function (result) {
                        layer.alert(result.msg);
                        window.location.reload();
                    }
                });
            }
        }
        if (obj.event === 'sj') {
            let index = layer.open({
                title: '升级设备',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: 'DeviceBatchUpgrading.html',
                success : function(layero, index){
                    let body = layui.layer.getChildFrame('body', index);
                    body.contents().find("[name=deviceid]").val(data.deviceid);
                    form.render();
                }
            });
            layer.full(index);
            return false;
        }
    });
});
